#include "Card.h"
#include "globalVariables.h"
#include "DataDefine.h"

int Card::paintTexture(SDL_Texture* &texture)
{
	SDL_Rect picRect = { CARD_PIC_X, CARD_PIC_Y, CARD_PIC_WIDTH, CARD_PIC_HEIGHT };
	//base render
	SDL_Surface* base = SDL_CreateRGBSurfaceWithFormat(0, CARD_WIDTH, CARD_HEIGHT, 32, SDL_PIXELFORMAT_RGBA8888);
	SDL_BlitSurface(cardSurface, NULL, base, NULL);
	//card name render
	SDL_Surface* surface = TTF_RenderUNICODE_Blended(titleFont, (Uint16*)((*name).c_str()), colorBlack);
	SDL_Texture* tempTexture = SDL_CreateTextureFromSurface(renderer, surface);
	int texW = 0;
	int texH = 0;
	SDL_QueryTexture(tempTexture, NULL, NULL, &texW, &texH);
	SDL_Rect titlerect = { CARD_TITLE_X+CARD_GAP, CARD_TITLE_Y+(CARD_TITLE_HEIGHT-texH)/2, texW, texH };
	SDL_BlitSurface(surface, NULL, base, &titlerect);

	SDL_FreeSurface(surface);
	SDL_DestroyTexture(tempTexture);
	//card description render
	int descCount = 0;
	for (wstring wstr : *description)
	{
		surface = TTF_RenderUNICODE_Blended(descFont, (Uint16*)(wstr.c_str()), colorBlack);
		tempTexture = SDL_CreateTextureFromSurface(renderer, surface);
		SDL_QueryTexture(tempTexture, NULL, NULL, &texW, &texH);
		SDL_Rect descrect = { CARD_DESC_X+CARD_GAP, CARD_DESC_Y+CARD_GAP+descCount*texH, texW, texH };
		SDL_BlitSurface(surface, NULL, base, &descrect);

		SDL_FreeSurface(surface);
		SDL_DestroyTexture(tempTexture);
		descCount++;
	}
	//card cost render
	surface = TTF_RenderUNICODE_Blended(costFont, (Uint16*)(to_string(cost).c_str()), colorBlack);
	tempTexture = SDL_CreateTextureFromSurface(renderer, surface);
	SDL_QueryTexture(tempTexture, NULL, NULL, &texW, &texH);
	SDL_Rect costrect = {CARD_COST_X+(CARD_COST_WIDTH-texW)/2, CARD_COST_Y+(CARD_COST_HEIGHT-texH)/2, texW, texH };
	SDL_BlitSurface(surface, NULL, base, &costrect);

	SDL_FreeSurface(surface);
	SDL_DestroyTexture(tempTexture);
	//card picture render
	SDL_BlitSurface(*picture, NULL, base, &picRect);
	//assign
	texture = SDL_CreateTextureFromSurface(renderer, base);
	//release
	SDL_FreeSurface(base);
	return 0;
}

int Card::paint(SDL_Renderer* renderer, int x, int y)
{
	SDL_Rect rect = {x, y, CARD_WIDTH, CARD_HEIGHT};
	SDL_RenderCopy(renderer, texture, NULL, &rect);
	return 0;
}

int Card::paintSmall(SDL_Renderer* renderer, int x, int y)
{
	SDL_Rect rect = { x, y, CARD_WIDTH_SMALL, CARD_HEIGHT_SMALL };
	SDL_RenderCopy(renderer, texture, NULL, &rect);
	return 0;
}

int Card::paintBack(SDL_Renderer* renderer, int x, int y)
{
	SDL_Rect rect = { x, y, CARD_WIDTH_SMALL, CARD_HEIGHT_SMALL };
	SDL_RenderCopy(renderer, cardBackTexture, NULL, &rect);
	return 0;
}

bool Card::canActivate()
{
	if (cost > player->currentCost)
	{
		return false;
	}
	for (int i = 0; i < 7; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			if (canSelect(i, j))
			{
				return true;
			}
		}
	}
	return false;
}

bool Card::canSelect(int x, int y)
{
	return true;
}

bool Card::operator==(const Card& op2) const
{
	return op2.id == id;
}